package com.ruoyi.web.controller.hotel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.domain.DishOrder;
import com.ruoyi.hotel.domain.DishOrderDetail;
import com.ruoyi.hotel.domain.DishesInfo;
import com.ruoyi.hotel.service.IDishOrderDetailService;
import com.ruoyi.hotel.service.IDishOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * 
 * @author chen
 * @date 2022-05-03
 */
@RestController
@RequestMapping("/hotel/order")
public class DishOrderController extends BaseController
{
    @Autowired
    private IDishOrderService dishOrderService;
    @Resource
    private IDishOrderDetailService dishOrderDetailService;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DishOrder dishOrder)
    {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long id = user.getUserId();
        List<SysRole> roles = user.getRoles();
        boolean flag = false;
        for (SysRole role : roles) {
            if(100 == role.getRoleId() || 2 ==role.getRoleId()){
                flag = true;
            }
        }
        if(flag){
            dishOrder.setCreateBy(id+"");
        }
        List<DishOrder> list = dishOrderService.selectDishOrderList(dishOrder);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DishOrder dishOrder)
    {
        List<DishOrder> list = dishOrderService.selectDishOrderList(dishOrder);
        ExcelUtil<DishOrder> util = new ExcelUtil<DishOrder>(DishOrder.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dishOrderService.selectDishOrderById(id));
    }

    /**
     * 新增
     */
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DishOrder dishOrder)
    {
        List<DishesInfo> dishesInfos = dishOrder.getDishesInfo();
        dishOrderService.insertDishOrder(dishOrder);
        for (DishesInfo dishesInfo : dishesInfos) {
            DishOrderDetail dishOrderDetail = new DishOrderDetail();
            dishOrderDetail.setDishId(Long.parseLong(dishesInfo.getId()));
            dishOrderDetail.setDishNum((long) dishesInfo.getNum());
            dishOrderDetail.setOrderId(dishOrder.getId());
            dishOrderDetail.setPrice(dishesInfo.getDishesPrice());
            dishOrderDetail.setTotalPrice(dishesInfo.getTotalPrice());
            dishOrderDetailService.insertDishOrderDetail(dishOrderDetail);
        }
        return toAjax(1);
    }

    /**
     * 修改
     */
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DishOrder dishOrder)
    {
        return toAjax(dishOrderService.updateDishOrder(dishOrder));
    }

    /**
     * 删除
     */
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dishOrderService.deleteDishOrderByIds(ids));
    }

}
